import time

from rest_framework import serializers
from .models import User,ValidCode
from .utils import *


# 反序列化添加用户数据
class UserUnserializer(serializers.Serializer):
    email = serializers.CharField(max_length=30)
    password = serializers.CharField(max_length=255)
    nickname = serializers.CharField(max_length=30)

    def create(self, data):
        data['password'] = encrypt(data.pop('password'))
        return User.objects.create(**data)


# 反序列化添加验证码
class CodeUnserializer(serializers.Serializer):
    email = serializers.CharField(max_length=60)
    code = serializers.CharField(max_length=60)
    losetime = serializers.DecimalField(
        decimal_places=8,
        max_digits=20,
        default=time.time() + 300)

    def create(self, data):
        return ValidCode.objects.create(**data)


